package com.xpsoft.oa.service.produce.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.produce.ProjectWorkPlanDao;
import com.xpsoft.oa.dao.produce.ProjectWorkPlanInfoDao;
import com.xpsoft.oa.model.produce.ProjectWorkPlan;
import com.xpsoft.oa.model.produce.ProjectWorkPlanInfo;
import com.xpsoft.oa.service.produce.ProjectWorkPlanService;

public class ProjectWorkPlanServiceImpl extends BaseServiceImpl<ProjectWorkPlan> implements ProjectWorkPlanService{
	private ProjectWorkPlanDao dao;
	@Autowired
	private ProjectWorkPlanInfoDao projectWorkPlanInfoDao;
	public ProjectWorkPlanServiceImpl(ProjectWorkPlanDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveProjectWorkPlan(ProjectWorkPlan projectWorkPlan,
			String dataList) {
		// TODO Auto-generated method stub
		dao.save(projectWorkPlan);
		Boolean isNew = true;//新增标志
		if(projectWorkPlan.getId()!=null){
			isNew = false;
		}
		dao.save(projectWorkPlan);
		if(StringUtils.isNotBlank(dataList)){
			List<ProjectWorkPlanInfo> projectWorkPlanInfoList = JsonUtil.fromJsonTypes(dataList,
					new TypeToken<List<ProjectWorkPlanInfo>>() {
					}.getType());
			if(!isNew){//修改
				List<Long> idList = new ArrayList<Long>();
				for(ProjectWorkPlanInfo projectWorkPlanInfo:projectWorkPlanInfoList){
					if(projectWorkPlanInfo.getId()!=null){//修改的记录
						idList.add(projectWorkPlanInfo.getId());
					}
				}
				if(idList.size()>0){//先进行删除
					projectWorkPlanInfoDao.deleteProjectWorkPlanInfos(projectWorkPlan.getId(), idList);
				}
			}
			for(ProjectWorkPlanInfo projectWorkPlanInfo:projectWorkPlanInfoList){
				projectWorkPlanInfo.setProjectWorkPlan(projectWorkPlan);
				projectWorkPlanInfoDao.save(projectWorkPlanInfo);
			}
		}
	}
}